﻿using System;
using System.Windows.Input;
using PSTS.Models;
using PSTS.UI.Commands;

namespace PSTS.UI.ViewModels
{
    public class LoginPageViewModel:ViewModelBase
    {
        private string mLogin = "";
        public string Login
        {
            get { return mLogin; }
            set 
            {
                if (mLogin != value)
                {
                    mLogin = value;
                    OnPropertyChanged("Login");
                }
            }
        }

        private string mPassword = "";
        public string Password
        {
            get { return mPassword; }
            set
            {
                if (mPassword != value)
                {
                    mPassword = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        private string mErrorMessage = string.Empty;
        public string ErrorMessage
        {
            get { return mErrorMessage; }
            set
            {
                if (mErrorMessage != value)
                {
                    mErrorMessage = value;
                    OnPropertyChanged("ErrorMessage");
                }
            }
        }

        private DelegateCommand mLogIn;
        public ICommand LogIn
        {
            get
            {
                if (mLogIn == null)
                {
                    mLogIn = new DelegateCommand(delegate { VerifyCredentials(); });
                }
                return mLogIn;
            }
        }

        private void VerifyCredentials()
        {
            if (Login != Constants.Login || Password != Constants.Password)
            {
                ErrorMessage = string.Empty;
                if (Login != Constants.Login)
                {
                    ErrorMessage += "Невірний логін.";
                }
                if (Password != Constants.Password)
                {
                    ErrorMessage += "\tНевірний пароль.";
                }
                return;
            }
            OnVerified();
        }

        public event EventHandler Verified;

        protected virtual void OnVerified()
        {
            EventHandler handler = Verified;
            if (handler != null) handler(this, EventArgs.Empty);
        }
    }
}
